package com.vf.admin.client.dao;

import com.vf.admin.common.db.BaseDao;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.AppMingXiShouLi;
import com.vf.admin.miaodong.entity.AppMingXiSongLi;
import com.vf.admin.utils.StringEx;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author anxiang
 * @version 1.0.0
 * @ClassName AppMingXiSongLiDao.java
 * @Description TODO
 * @createTime 2021年11月02日 15:22:00
 */
@Repository
public class AppMingXiSongLiDao extends BaseDao<AppMingXiSongLi> {

    public void saveOrUpdate(AppMingXiSongLi appMingXiSongLi) {
        if (appMingXiSongLi.getId() == null || appMingXiSongLi.getId() == 0) {
            appMingXiSongLi.setId(this.add(appMingXiSongLi));
        } else {
            this.update(appMingXiSongLi);
        }
    }


    //查找指定时间获取金豆的排行榜
    public List<AppMingXiSongLi> leaderBoard(String startTime, String endTime) {
        if (StringEx.isNull(startTime)) {
            return null;
        }
        if (StringEx.isNull(endTime)) {
            return null;
        }
        StringBuilder str = new StringBuilder();
        List<Object> list = new ArrayList<>();
        str.append("select *,sum(fujianum) as total from app_mingxi_songli  where createtime >= ? and createtime <= ? GROUP BY userid  ORDER BY total desc");
        list.add(startTime);
        list.add(endTime);
        String sql = str.toString();
        return this.queryList(sql, list, AppMingXiSongLi.class);
    }

    /**
     * 更新礼物信息
     */
    public void updateGiftData(Integer giftid,String picture,String giftname) {
        if(StringEx.isNull(giftid)){
            return ;
        }
        ArrayList<Object> paramList = new ArrayList<Object>();
        StringBuffer sql =new StringBuffer(" update app_mingxi_songli set giftpic=? ") ;
        paramList.add(picture);
        sql.append(" ,giftname=? ");
        paramList.add(giftname);
        sql.append(" where giftid=? ");
        paramList.add(giftid);
        this.update(sql.toString(),paramList);
    }

    //查找用户送礼明细
    public List<AppMingXiSongLi> findList(Integer userid, SplitPageBean splitPageBean) {
        if (StringEx.isNull(userid)) {
            return null;
        }
        StringBuilder str = new StringBuilder();
        List<Object> list = new ArrayList<>();
        str.append("select * from app_mingxi_songli where 1=1 ");
        str.append(" and userid = ? ");
        list.add(userid);
        str.append(" order by createtime desc ");
        String sql = str.toString();
        return this.queryList(sql, list, AppMingXiSongLi.class, splitPageBean);
    }

    public List<AppMingXiSongLi> list(Map<String, Object> params) {
        StringBuilder str = new StringBuilder();
        List<Object> list = new ArrayList<>();
        str.append("select * from app_mingxi_songli where 1=1 ");
        if (StringEx.isNotNull(params.get("userid"))) {
            str.append(" and userid = ? ");
            list.add(params.get("userid"));
        }
        if(params.containsKey("userids") && StringEx.isNotNull(params.get("userids"))){
            str.append(" and userid in ( ");
            str.append(params.get("userids"));
            str.append(" ) ");
        }
        if (StringEx.isNotNull(params.get("nick"))) {
            str.append(" and nick like '%").append(params.get("nick")).append("%'");
        }
        if (StringEx.isNotNull(params.get("phone"))) {
            str.append(" and phone like '%").append(params.get("phone")).append("%'");
        }
        if (StringEx.isNotNull(params.get("giftname"))) {
            str.append(" and giftname like '%").append(params.get("giftname")).append("%'");
        }
        if(StringEx.isNotNull(params.get("startDate"))){
            str.append(" and DATE_FORMAT(createtime, '%Y-%m-%d')  >=  ? ");
            list.add(params.get("startDate"));
        }
        if(StringEx.isNotNull(params.get("endDate"))){
            str.append(" and DATE_FORMAT(createtime, '%Y-%m-%d')  <=  ? ");
            list.add(params.get("endDate"));
        }
        str.append(" order by createtime desc ");
        String sql = str.toString();
        return this.queryList(sql, list, AppMingXiSongLi.class);
    }

    public List<AppMingXiSongLi> list(Map<String, Object> params, SplitPageBean splitPageBean) {
        StringBuilder str = new StringBuilder();
        List<Object> list = new ArrayList<>();
        str.append("select * from app_mingxi_songli where 1=1 ");
        if (StringEx.isNotNull(params.get("userid"))) {
            str.append(" and userid = ? ");
            list.add(params.get("userid"));
        }
        if(params.containsKey("userids") && StringEx.isNotNull(params.get("userids"))){
            str.append(" and userid in ( ");
            str.append(params.get("userids"));
            str.append(" ) ");
        }
        if (StringEx.isNotNull(params.get("nick"))) {
            str.append(" and nick like '%").append(params.get("nick")).append("%'");
        }
        if (StringEx.isNotNull(params.get("phone"))) {
            str.append(" and phone like '%").append(params.get("phone")).append("%'");
        }
        if (StringEx.isNotNull(params.get("giftname"))) {
            str.append(" and giftname like '%").append(params.get("giftname")).append("%'");
        }
        if(StringEx.isNotNull(params.get("startDate"))){
            str.append(" and DATE_FORMAT(createtime, '%Y-%m-%d')  >=  ? ");
            list.add(params.get("startDate"));
        }
        if(StringEx.isNotNull(params.get("endDate"))){
            str.append(" and DATE_FORMAT(createtime, '%Y-%m-%d')  <=  ? ");
            list.add(params.get("endDate"));
        }
        str.append(" order by createtime desc ");
        String sql = str.toString();
        return this.queryList(sql, list, AppMingXiSongLi.class, splitPageBean);
    }

    public List<AppMingXiSongLi> selectResultByUserId(Integer id) {
        StringBuilder str = new StringBuilder();
        List<Object> list = new ArrayList<>();
        str.append("select * from app_mingxi_songli where 1=1");
        str.append(" and userid = ?");
        list.add(id);
        str.append(" and createtime >= DATE_FORMAT(now(), '%y-%m-%d 00:00:00') and createtime <= DATE_FORMAT(now(),'%y-%m-%d 23:59:59')");
        String sql = str.toString();
        return this.queryList(sql,list,AppMingXiSongLi.class);
    }

    public List<AppMingXiSongLi> getSongLiByUserId(Integer id, String format) {
        StringBuilder str = new StringBuilder();
        List list = new ArrayList();
        str.append("select * from app_mingxi_songli where userid = ? ");
        list.add(id);
        str.append(" and createtime >= ? ");
        list.add(format);
        String sql = str.toString();
        return this.queryList(sql,list,AppMingXiSongLi.class);
    }

    public List<AppMingXiSongLi> selectSongLiByUserId(Integer id) {
        StringBuilder str = new StringBuilder();
        List<Object> list = new ArrayList<>();
        str.append("select * from app_mingxi_songli where ");
        str.append("userid = ? ");
        list.add(id);
        str.append(" and createtime >= DATE_FORMAT(now(), '%y-%m-%d 00:00:00') and createtime <= DATE_FORMAT(now(),'%y-%m-%d 23:59:59')");
        String sql = str.toString();
        return this.queryList(sql,list,AppMingXiSongLi.class);
    }

    public List<AppMingXiSongLi> selectSevenDaySongLiByUserId(Integer id, String format1) {
        StringBuilder str = new StringBuilder();
        List<Object> list = new ArrayList<>();
        str.append("select * from app_mingxi_songli where ");
        str.append(" userid = ? ");
        list.add(id);
        str.append(" and createtime >= ?");
        list.add(format1);
        String sql = str.toString();
        return this.queryList(sql,list,AppMingXiSongLi.class);
    }


    public List<AppMingXiSongLi> getDayRanking(Map<String, Object> params)
    {
        ArrayList<Object> list = new ArrayList<>();
        StringBuilder sb = new StringBuilder(" select *, sum(ifnull(fujianum,0)) as totalMoney from app_mingxi_songli where to_days(createtime) = to_days(now())");
        if(params.containsKey("hiddenUserIds") && StringEx.isNotNull(params.get("hiddenUserIds"))) {
            sb.append(" AND userid not in ("+ params.get("hiddenUserIds") +")");
        }
        sb.append(" GROUP BY userid ORDER BY totalMoney desc limit 100");

        return this.queryList(sb.toString(),list, AppMingXiSongLi.class);
    }

    public List<AppMingXiSongLi> getWeekRanking(Map<String, Object> params)
    {
        ArrayList<Object> list = new ArrayList<>();
        StringBuilder sb = new StringBuilder(" select *, sum(ifnull(fujianum,0)) as totalMoney from app_mingxi_songli where YEARWEEK( date_format(  createtime,'%Y-%m-%d' ) ) = YEARWEEK( now() )");
        if(params.containsKey("hiddenUserIds") && StringEx.isNotNull(params.get("hiddenUserIds"))) {
            sb.append(" AND userid not in ("+ params.get("hiddenUserIds") +")");
        }

        sb.append("GROUP BY userid ORDER BY totalMoney desc limit 100");
        return this.queryList(sb.toString(),list, AppMingXiSongLi.class);
    }

    public List<AppMingXiSongLi> getMonthRanking(Map<String, Object> params)
    {
        ArrayList<Object> list = new ArrayList<>();
        StringBuilder sb = new StringBuilder(" select *, sum(ifnull(fujianum,0)) as totalMoney from app_mingxi_songli where DATE_FORMAT( createtime, '%Y%m' ) = DATE_FORMAT( CURDATE( ) , '%Y%m' )");
        if(params.containsKey("hiddenUserIds") && StringEx.isNotNull(params.get("hiddenUserIds"))) {
            sb.append(" AND userid not in ("+ params.get("hiddenUserIds") +")");
        }

        sb.append(" GROUP BY userid ORDER BY totalMoney desc limit 100");
        return this.queryList(sb.toString(),list, AppMingXiSongLi.class);
    }


}
